package com.rjj.wlqq.pdf;

import com.jfinal.template.Engine;
import com.rjj.wlqq.tool.io.ResponseDownOutPutStream;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Controller
public class PdfController {
    private static final String DESTJfinal = "target/pdfJfinal.pdf";
    private static final String DESTFreemarker = "target/pdfFreemarker.pdf";
    private static final String DESTVelocity = "target/pdfVelocity.pdf";

    @RequestMapping("/pdf")
    public String pdf() {
        return "pdf/pdf";
    }

    /**下载*/
    @RequestMapping("/downloadPdf")
    public void downloadPdf(HttpServletRequest request,HttpServletResponse response) {

        Engine myEngine = Engine.use();
        TemplateToPDFJfinal.setEngine(myEngine);
        try {
            //响应的数据页面预览了  test("templateJfinal2.html", TemplateToPDFJfinal.class,response.getOutputStream());
            String fileName = "pdf.pdf";
            test("templateJfinal2.html", TemplateToPDFJfinal.class, ResponseDownOutPutStream.getResponseOutputStream(request,response,fileName));
            //test("templateFreemarker.html",TemplateToPDFFreemarker.class,response.getOutputStream());
            //test("templateVelocity.html",TemplateToPDFVelocity.class,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**预览*/
    @RequestMapping("/previewPdf")
    public void previewPdf(HttpServletResponse response) {

        try {
            test("templateJfinal2.html", TemplateToPDFJfinal.class,response.getOutputStream());
            //test("templateFreemarker.html",TemplateToPDFFreemarker.class,response.getOutputStream());
            //test("templateVelocity.html",TemplateToPDFVelocity.class,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws FileNotFoundException {
        Engine myEngine = Engine.create("myEngine");
        TemplateToPDFJfinal.setEngine(myEngine);
        //test("templateJfinal2.html", TemplateToPDFJfinal.class,new FileOutputStream(new File(DESTJfinal)));
        test("templateFreemarker.html",TemplateToPDFFreemarker.class,new FileOutputStream(new File(DESTFreemarker)));
        test("templateVelocity.html",TemplateToPDFVelocity.class,new FileOutputStream(new File(DESTVelocity)));
    }

    /**
     * 简单写个方法测试 生成pdf
     *
     * @param templateName 模板名称
     * @param pdfClass     使用的是哪个实现类
     */
    private static void test(String templateName, Class<?> pdfClass, OutputStream outputStream) {
        String FONT = "static/pdf/font/simhei.ttf";
        BasePDF.setFont(FONT);
        PDF pdf = PDFFactory.getPDF(pdfClass);
        Map<String, Object> map = new HashMap<>();
        map.put("name", "中国社会主义价值核心观");
        map.put("imgUrl", "/Users/renjianjun/Downloads/我的/斗图/mao.jpg");
        boolean b = pdf.writeToPDF(map, templateName, outputStream);
        System.out.println("输出结果：" + b);
    }


    /**
     * 测试获取的路径
     */
    @Test
    public void getPath() throws FileNotFoundException {
        String path = ClassUtils.getDefaultClassLoader().getResource("templates/pdf/templateJfinal.html").getPath();
        System.out.println(path);

        String path1 = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
        System.out.println(path1);

        String path2 = "/templates/pdf/templateJfinal.html";//最开始的 /  加不加都行
        ClassPathResource classPathResource = new ClassPathResource(path2);

        try {
            System.out.println(classPathResource.getURL().getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
